package com.ls.controller.a;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.ls.common.BaseController;
import com.ls.common.entity.JsonResult;
import com.ls.model.Role;
import com.ls.model.RolePermission;
import com.ls.service.RolePermissionService;
import com.ls.service.RoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p></p>
 * Created by zhezhiyong@163.com on 2017/2/9.
 */
@RestController
@RequestMapping("/role")
public class RoleController extends BaseController {

    @Resource
    private RoleService roleService;
    @Resource
    private RolePermissionService rolePermissionService;

    @RequiresPermissions("role:view")
    @RequestMapping("/list")
    public JSONObject list(@RequestParam(required = false) Integer page, @RequestParam(required = false) Integer rows) {
        JSONObject object = new JSONObject();
        List<Role> roleList = roleService.list(page, rows);
        object.put("rows", roleList);
        object.put("total", ((Page) roleList).getTotal());
        return object;
    }

    @RequestMapping("/select")
    public List<Role> list() {
        return roleService.all();
    }

    @RequestMapping(value = "/{id}")
    public Role get(@PathVariable Integer id) {
        return roleService.getById(id);
    }

    @RequiresPermissions("role:save")
    @PostMapping(value = "/save")
    public JsonResult save(Role role) {
        role.setUpdateTime(new Date());
        roleService.save(role);
        return JsonResult.SUCCESS;
    }

    @RequiresPermissions("role:update")
    @PostMapping(value = "/update/{id}")
    public JsonResult update(@PathVariable int id, Role role, HttpServletRequest request) {
        role.setId(id);
        String ids = request.getParameter("ids");
        List<String> pIds = Arrays.asList(ids.split(","));
        rolePermissionService.deleteByRoleId(id);
        pIds.stream().
                filter(pid -> (null != pid && !"".equals(pid))).
                forEach(pid -> rolePermissionService.save(new RolePermission(id, Integer.valueOf(pid))));
        roleService.update(role);
        return JsonResult.SUCCESS;
    }

    @RequiresPermissions("role:delete")
    @PostMapping(value = "/delete/{id}")
    public JsonResult delete(@PathVariable int id) {
        roleService.delete(id);
        return JsonResult.SUCCESS;
    }

//    @PostMapping(value = "/grant/{roleId}")
//    public JsonResult grant(@PathVariable int roleId, HttpServletRequest request) {
//        String id = request.getParameter("id");
//        String[] ids = id.split(",");
//        List<String> pIds = Arrays.asList(ids);
//        rolePermissionService.deleteByRoleId(roleId);
//        pIds.forEach(lid-> rolePermissionService.save(new RolePermission(roleId, Integer.valueOf(lid))));
//        return JsonResult.SUCCESS;
//    }

    @GetMapping(value = "/authorized/{roleId}")
    public JsonResult authorized(@PathVariable int roleId) {
        List<String> pIds = rolePermissionService.selectPermissionIdByRoleId(roleId);
        return new JsonResult(true, null, pIds);
    }

}
